﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RN.ERP.Web.BasicService;
using RN.ERP.Web.EnterpriseService;
using RN.ERP.Web.ModelService;
using RN.ERP.Web.PersonService;
using Newtonsoft.Json;
using System.Web.Services;
using Fly.Common.Linq;

namespace RN.ERP.Web
{
    public partial class Register : System.Web.UI.Page
    {
        private BasicServiceClient basicService = new BasicServiceClient();
        private EnterpriseServiceClient enterpriseService = new EnterpriseServiceClient();
        public override void VerifyRenderingInServerForm(Control control)        
        {            // Confirms that an HtmlForm control is rendered for       
        }
     
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDDLCorpType();
                BindCorpUserType();
            }
        }

        public int checkUserName()
        {
            return 0;
        }

        private void BindDDLCorpType()
        {
            //企业类型
            string result_Type = basicService.GetDictionary("CorpType", "", "02");
            if (string.IsNullOrEmpty(result_Type))
            {
                return;
            }
            Dictionary<string, string> dicCorpType = JsonConvert.DeserializeObject<Dictionary<string, string>>(result_Type);
            this.DDlCorpType.DataSource = dicCorpType;
            this.DDlCorpType.DataValueField = "Key";
            this.DDlCorpType.DataTextField = "Value";
            this.DDlCorpType.DataBind();
            this.DDlCorpType.Items.Insert(0, new ListItem("-请选择-", ""));
        }

        /// <summary>
        /// 绑定企业类型
        /// </summary>
        private void BindCorpUserType()
        {
            ////获取所有企业类型
            Dictionary<string, string> dicCorpUserType = GetCorpUserType();
            DDLCorpUserType.DataSource = dicCorpUserType;
            this.DDLCorpUserType.DataValueField = "Key";
            this.DDLCorpUserType.DataTextField = "Value";
            DDLCorpUserType.DataBind();
            DDLCorpUserType.Items.Insert(0, new ListItem("--请选择--", ""));
        }

        private Dictionary<string, string> GetDdlByDic(string type, string condition)
        {
            string result = Application[ApplicationConstant.APPLICATION_CORPUSERTYPE] == null ? string.Empty : Application[ApplicationConstant.APPLICATION_CORPUSERTYPE].ToString();
            if (string.IsNullOrEmpty(result))
            {
                return new Dictionary<string, string>();
            }
            return JsonConvert.DeserializeObject<Dictionary<string, string>>(result);
        }

        private Dictionary<string, string> GetCorpUserType()
        {
            Dictionary<string, string> ret = new Dictionary<string, string>();
            string appId = Application[ApplicationConstant.APPLICATION_ID] == null ? string.Empty : Application[ApplicationConstant.APPLICATION_ID].ToString();
            if (appId == "101")
            {
                ret.Add("2501","施工企业");
                ret.Add("2506","装饰企业");
                ret.Add("2301","门窗企业");
                ret.Add("30","商砼企业");
            }
            else
            {
                ret = GetDdlByDic("CorpUserType", "");
            }
            return ret;
        }

        #region Ajax

        [WebMethod]
        public static string VerifyCorpReg(string userName,string orgCode,string corpType,string dogId)
        {
           EnterpriseServiceClient enterpriseService = new EnterpriseServiceClient();
            int retUser=0;
            int retOrg = 0;
            //验证用户名
            bool verifyUser = enterpriseService.IsUserExisted(userName,string.Empty);
            if (verifyUser == true)
            {
                retUser = -1;
            }
            else
            {
                retUser = 1;
            }
            //验证组织机构代码
            bool verifyOrgCode = false;
            if (!String.IsNullOrEmpty(dogId))
            {
                verifyOrgCode = enterpriseService.IsOrgnizationCodeExists(orgCode, corpType, "");
            }
            else
            {
                EnterpriseService.EnterpriseServiceClient enterpriseServiceClient = new EnterpriseService.EnterpriseServiceClient();
                QueryCompanyInfoCondition condition = new QueryCompanyInfoCondition();
                condition.OrginazationCode = new ParameterValue<string>() { V = orgCode, O = OptEnum.Equal };
                List<QueryCompanyInfoCondition> list = new List<QueryCompanyInfoCondition>();
                list.Add(condition);
                var result = enterpriseServiceClient.GetCompaniesInfo(JsonConvert.SerializeObject(list), "");
                var corpInfo = JsonConvert.DeserializeObject<List<Tb_Corp_CompanyInfo>>(result);
                ////2014-4-18 烟台引入招标代理企业（roleid=10506,classid=801006），与原来的代理角色不同，因此允许新旧代理企业同时存在，但不允许相同角色的存在两家
                ////判断该类型企业是否已经存在
                if (corpType == "10506")
                {
                    BasicServiceClient basicServiceClient = new BasicServiceClient();
                    var roleClassIds = JsonConvert.DeserializeObject<Dictionary<string, string>>(basicServiceClient.GetDictionary("RoleClassId", "", ""));
                    var curRoleClass = roleClassIds.Keys.Contains(corpType) ? roleClassIds[corpType] : "";
                    int classId = -1;
                    if (int.TryParse(corpType, out classId) && corpInfo.Where(c => c.classID == classId).Count() > 0)
                    {
                        verifyOrgCode = true;
                    }
                }
                else if (corpInfo != null && corpInfo.Count > 0)
                {
                    verifyOrgCode = true;
                }
            }
            if (verifyOrgCode == true)
            {
                retOrg = -2;
            }
            else
            {
                retOrg = 2;
            }
            
            //string output = "{{\"status1\":\"" + retUser.ToString() + "\",\"status2\":\"" + retOrg.ToString() + "\"}}";
            string output = JsonConvert.SerializeObject(new {status1=retUser,status2=retOrg });
            return output;
        }

        [WebMethod]
        public static string VerifyExpertReg(string exMobile, string idCard)
        {
            int retMobileNum = 0;
            int retIdCard = 0;
            ////验证手机号
            using (PersonServiceClient personServiceClient = new PersonServiceClient())
            {
                QueryPersonInfoCondition condition = new QueryPersonInfoCondition();
                condition.MobileTel = new ParameterValue<string>() { V = exMobile, O = OptEnum.Equal };
                List<QueryPersonInfoCondition> list = new List<QueryPersonInfoCondition>();
                list.Add(condition);
                int empNum = personServiceClient.GetEmpManagerListCount(JsonConvert.SerializeObject(list), string.Empty);
                if (empNum > 0)
                {
                    retMobileNum = -1;
                }
                else
                {
                    retMobileNum = 1;
                }
            }
            ////验证专家身份证号码
            bool verifyIdCard = false;
            using (PersonServiceClient personServiceClient = new PersonServiceClient())
            {
                QueryPersonInfoCondition condition = new QueryPersonInfoCondition();
                condition.IDCard = new ParameterValue<string>() { V = idCard, O = OptEnum.Equal };
                List<QueryPersonInfoCondition> list = new List<QueryPersonInfoCondition>();
                list.Add(condition);
                int empNum = personServiceClient.GetEmpManagerListCount(JsonConvert.SerializeObject(list), string.Empty);
                if (empNum > 0)
                {
                    verifyIdCard = true;
                }
                if (verifyIdCard == true)
                {
                    retIdCard = -2;
                }
                else
                {
                    retIdCard = 2;
                }
                string output = JsonConvert.SerializeObject(new { status1 = retMobileNum, status2 = retIdCard });
                return output;
            }

        }
        #endregion
    }

}